package com.zandequan.modules.article.controller;

import java.util.List;
import java.util.Map;

import com.zandequan.common.validator.ValidatorUtils;
import com.zandequan.common.validator.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zandequan.modules.article.entity.MediaEntity;
import com.zandequan.modules.article.service.MediaService;
import com.zandequan.common.utils.PageUtils;
import com.zandequan.common.utils.Query;
import com.zandequan.common.utils.R;




/**
 * 文章_媒体表，图片，视频，音频，附件
 * 
 * @author treesea
 * @email 2117999344@qq.com
 * @date 2017-08-13 00:01:18
 */
@RestController
@RequestMapping("media")
public class MediaController {
	@Autowired
	private MediaService mediaService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("media:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<MediaEntity> mediaList = mediaService.queryList(query);
		int total = mediaService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(mediaList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{articleMediaId}")
	@RequiresPermissions("media:info")
	public R info(@PathVariable("articleMediaId") Integer articleMediaId){
		MediaEntity media = mediaService.queryObject(articleMediaId);
		
		return R.ok().put("media", media);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("media:save")
	public R save(@RequestBody MediaEntity media){
		ValidatorUtils.validateEntity(media, UpdateGroup.class);

		mediaService.save(media);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("media:update")
	public R update(@RequestBody MediaEntity media){
		mediaService.update(media);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("media:delete")
	public R delete(@RequestBody Integer[] articleMediaIds){
		mediaService.deleteBatch(articleMediaIds);
		
		return R.ok();
	}
	
}
